package com.ebelter.ty.ui.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.ebelter.btcomlib.utils.DipUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;


public class HoriLineView extends View {
    public static final String TAG = "HoriLineView";

    private int dp2px(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
    private int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
    public HoriLineView(Context context) {
        this(context,null);
    }
    public HoriLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }




    Paint mPaint;
    Paint mPaint2;
    Path mDashPath;
    int mWidth;
    int mHeight;
    int lineHeight;


    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.parseColor("#fec724"));
        mPaint.setStrokeWidth(dp2px(getContext(),1));
        mPaint.setPathEffect(new DashPathEffect(new float[] {5,5,5,5}, 1));
        mPaint.setTextSize(DipUtil.sp2px(getContext(),6));

        mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mPaint2.setStyle(Paint.Style.STROKE);
        mPaint2.setColor(Color.parseColor("#fdc824"));

        mPaint2.setTextSize(DipUtil.sp2px(getContext(),6));

        mDashPath=new Path();

        textWidth1=getTextWidth(mPaint,"X");
        textWidth2=getTextWidth(mPaint,"XX");
        textWidth3=getTextWidth(mPaint,"XXX");
        textWidth4=getTextWidth(mPaint,"XXXX");
        textWidth5=getTextWidth(mPaint,"XXXXX");

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMySize(100, widthMeasureSpec);
        int height = getMySize(100, heightMeasureSpec);
        setMeasuredDimension(width,height);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth=getMeasuredWidth();
        mHeight=getMeasuredHeight();
        lineHeight= (int) (mHeight*0.9);//0.9是看UI图比例计算出来的
    }





    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawTargetValue(canvas);//画目标线
    }

    private boolean display;
    private float percentage;
    private String displayText;

    public void show(){
        invalidate();
    }

    public boolean isDisplay() {
        return display;
    }

    public void setDisplay(boolean display) {
        this.display = display;
    }

    public float getPercentage() {
        return percentage;
    }

    public void setPercentage(float percentage) {
        this.percentage = percentage;
    }

    public String getDisplayText() {
        return displayText;
    }

    public void setDisplayText(String displayText) {
        this.displayText = displayText;
    }

    //绘制目标值
    private void drawTargetValue(Canvas canvas) {
        if (display&&!TextUtils.isEmpty(displayText)){




            int stopY2= (int) (lineHeight*(1-percentage));
            mDashPath.reset();
            mDashPath.moveTo(0,stopY2);
            mDashPath.lineTo(mWidth,stopY2);
            canvas.drawPath(mDashPath,mPaint);



            if (percentage<0.5){
                stopY2=stopY2-textWidth2;
            }else {
                stopY2=stopY2+textWidth2;
            }

            LogUtils.i(TAG,"----displayText : "+displayText);
            canvas.drawText(displayText,mWidth-getTextWidth(displayText)*2,stopY2,mPaint2);
        }

    }



    private int getTextWidth(String string){
        if (TextUtils.isEmpty(string)){
            return 0;
        }
        switch (string.length()){
            case 1:
                return textWidth1;

            case 2:
                return textWidth2;

            case 3:
                return textWidth3;

            case 4:
                return textWidth4;

            case 5:
                return textWidth5;
            case 7:
                return textWidth7;
            case 10:
                return textWidth10;

            default:
                return textWidth1*string.length();
        }
    }





    private int getMySize(int defaultSize, int measureSpec) {
        int mySize = defaultSize;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);//这里的的尺寸大小并不是最终我们的View的尺寸大小，而是父View提供的参考大小
        switch (mode) {
            case MeasureSpec.UNSPECIFIED: {//如果没有指定大小，就设置为默认大小
                mySize = defaultSize;
                break;
            }
            case MeasureSpec.AT_MOST: {//如果测量模式是最大取值为size
                //我们将大小取最大值,你也可以取其他值
                mySize = size;
                break;
            }
            case MeasureSpec.EXACTLY: {//如果是固定的大小，那就不要去改变它
                mySize = size;
                break;
            }
        }
        return mySize;
    }


    int textWidth1;
    int textWidth2;
    int textWidth3;
    int textWidth4;
    int textWidth5;
    int textWidth7;
    int textWidth10;

    public int getTextWidth(Paint paint, String str) {
        int w= 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                w+= (int) Math.ceil(widths[j]);
            }
        }
        return w;
    }
}
